package io.yizoo.core.message;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import io.yizoo.core.LoginWorkWeixinService;
import io.yizoo.system.entity.sys_user_third;
import io.yizoo.system.util.HttpClientUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service("workWeixin")
public class WorkWeixinMessageService extends AbstractMessageService {

    @Autowired
    private LoginWorkWeixinService loginWorkWeixinService;
    @Autowired
    public io.elm.core.Ctor Ctor;


    @Override
    public String sendMsg(MessageContext context) {
        validParam(context);
        Boolean isAll = context.getAll();
        if (isAll == null) {
            isAll = false;
        }
        String toTargetStr = "";

        String sendUserId = context.getSendUserId();
        sys_user_third sysUserThird = Ctor.HMapper.GetSingle(sys_user_third.class,
                Ctor.DMaker().Selector
                        .From(sys_user_third.class)
                        .putFactorItem("id", sendUserId)
                        .IsWhere("and user_id = #{id}", sendUserId)
                        .Limit("limit 1")
                        .GetFactor());
        if (sysUserThird == null) {
            throw new RuntimeException("发送人员不存在");
        }
        if (isAll) {
            toTargetStr = "IGNORE_LEN";
        } else {
            context.setUserDelimiter("|");
            toTargetStr = getTargetUserIdListStr(context);
        }
        String corpId = sysUserThird.getCorp_id();
        Map<String, String> textCard = new HashMap<>();
        Map<String, Object> map = new HashMap<>();
        map.put("touser", toTargetStr);
        String agentId = loginWorkWeixinService.getAgentId(corpId);
        map.put("agentid", agentId);
        textCard.put("title", context.getTitle());
        textCard.put("description", context.getContent());
        textCard.put("url", "http://www.snbg365.com/v/system/login?loginType=workWeixin&corpid=$CORPID$");
        textCard.put("btntxt", "详情");
        map.put("textcard", textCard);
        map.put("msgtype", "textcard");

        String accessToken = loginWorkWeixinService.getAccessToken(corpId);
        if (accessToken == null) {
            return "获取accessToken失败";
        }
        String sendUrl = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=" + accessToken;
        String result = HttpClientUtils.doPost(sendUrl, map);
        System.out.println("发送卡片消息：" + result);
        JSONObject jsonObject = JSONObject.parseObject(result);
        if (jsonObject.getInteger("errcode") != 0) {
             throw new RuntimeException("发送卡片消息失败！ 错误信息：" + jsonObject.getString("errmsg"));
        }
        return "success";
    }




    public static void main(String[] args) {
        //String userIdsStr = String.join("', '", userIds);
        //userIdsStr = "'" + userIdsStr + "'";
        List<String> userIdList = Lists.newArrayList("zhangsan", "lisi");
        System.out.println(StringUtils.join(userIdList, "','"));
    }


}
